package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.HashMap;

/**
 * ClassName: AutoFillAspect
 * Package: com.sky.aspect
 * Description:
 *
 * @Author: @Albert
 * @Version: 1.0
 */
@Aspect
@Slf4j
@Component
//切面类  本身就是  通知加上切入点
public class AutoFillAspect {
    //切入点  pointcut
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut(){};

    //定义通知   五种通知
    @Before("autoFillPointCut()")
    //joinPoint  可以获得拦截方法的参数和方法名称
    public void autoFill(JoinPoint joinPoint){
        log.info("开始进行公共字段填充");
        //获取被拦截的操纵数据库方法的数据库操作类型   增删改查
        MethodSignature signature=(MethodSignature)joinPoint.getSignature();
        AutoFill autoFill= signature.getMethod().getAnnotation(AutoFill.class);
        OperationType operationType = autoFill.value();
        //获得被拦截方法的参数   ---  实体对象
        Object[] args = joinPoint.getArgs();
        if(args == null || args.length==0){
            return;
        }
        Object entity = args[0];
        //准备赋值的数据
        LocalDateTime time = LocalDateTime.now();
        Long userId = BaseContext.getCurrentId();
        //根据不同的操作类型,使用反射为对应的属性赋值
        if(operationType==OperationType.INSERT){
            try {
                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                //通过反射为属性赋值
                setCreateTime.invoke(entity,time);
                setUpdateTime.invoke(entity,time);
                setCreateUser.invoke(entity,userId);
                setUpdateUser.invoke(entity,userId);
            } catch (Exception e) {
                e.printStackTrace();
            }
            HashMap mam = new HashMap();
        }else{
            try {
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                //通过反射为属性赋值
                setUpdateTime.invoke(entity,time);
                setUpdateUser.invoke(entity,userId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }

}
